<?php

namespace app\index\model;

use Exception;
use frame\lib\Db;

class Fake
{
    public static function list($query = '')
    {
        $uid = $_SESSION['user']['id'];
        if ($query) {
            $list = Db::name('fake')->where(['delete_time' => 0, 'adder_user_id' => $uid])->like('fake', "%$query%")->select();
        } else {
            $list = Db::name('fake')->where(['delete_time' => 0, 'adder_user_id' => $uid])->select();
        }
        return $list;
    }
    public static function add($param)
    {
        $uid = $_SESSION['user']['id'];
        $fakeNum = Db::name('fake')->where(['adder_user_id' => $uid, 'delete_time' => 0])->count();
        $limit = Db::name('user_limit')->where(['uid' => $uid])->find();
        $nowNum = $param['num'] + $fakeNum;
        if ($limit['fake'] < $nowNum) error('当前');
        @$vip_time = vipTimeCompute(@$param['vip_type'], @$param['vip_count']);
        $data = [
            'vip_time' => $vip_time,
            'status' => 0,
            'type' =>  $param['type'],
            'score' =>  @$param['score'] ?? 0,
            'adder_user_id' => $_SESSION['user']['id'],
        ];
        for ($i = 0; $i < $param['num']; $i++) {
            $data['fake'] = randStr(20);
            $data['create_time'] = getTime();
            Db::name('fake')->insert($data);
        }
    }
}
